package suncertify.db.test;

import suncertify.contractors.Contractor;
import suncertify.db.DBAccess;
import suncertify.db.RecordNotFoundException;
import suncertify.remoting.RemoteContractorsAccessImpl;

public class LockUpdateUnlockUser extends Thread {

    private String userName;
    private DBAccess data;
    private int iterations;
    private long fromID;
    private long toID;

    public LockUpdateUnlockUser(String name, DBAccess data, long fromID, long toID, int iterations) {

	super.setName(name);
	this.userName = name;
	this.data = data;
	this.iterations = iterations;
	this.fromID = fromID;
	this.toID = toID;
    }

    public void run() {

	long lockCookie = -1;

	String[] fields;

	for (int i = 1; i <= this.iterations; ++i) {

	    // Data.log("", "================= Update user iteration " + Integer.toString(i) + " ==================");

	    for (long recordIndex = this.fromID; recordIndex <= this.toID; ++recordIndex) {

		try {

		    try {
			lockCookie = this.data.lockRecord(recordIndex);
		    } catch (RecordNotFoundException ex) {
			// Data.log("", "????????????????????? RecordNotFoundException recordID = "
			// + Long.toString(recordIndex));
			continue;
		    }

		    fields = new String[6];

		    fields[0] = this.userName + " " + Integer.toString(i);
		    fields[1] = this.userName + " " + Integer.toString(i);
		    fields[2] = this.userName + " " + Integer.toString(i);
		    fields[3] = "10";
		    fields[4] = "$20.00";
		    fields[5] = "12345678";

		    this.data.updateRecord(recordIndex, fields, lockCookie);
		    this.data.unlock(recordIndex, lockCookie);

		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	}
    }
}

class LockUpdateUnlockUser2 extends Thread {

    private String userName;
    private RemoteContractorsAccessImpl remoteContractorsAccess;
    private int iterations;
    private long fromID;
    private long toID;

    public LockUpdateUnlockUser2(String name, RemoteContractorsAccessImpl data, long fromID, long toID, int iterations) {

	super.setName(name);
	this.userName = name;
	this.remoteContractorsAccess = data;
	this.iterations = iterations;
	this.fromID = fromID;
	this.toID = toID;
    }

    public void run() {

	long lockCookie = -1;

	String[] fields;
	Contractor contractor;

	for (int i = 1; i <= this.iterations; ++i) {

	    // Data.log("", "================= LockUpdateUnlockUser2 user iteration " + Integer.toString(i)
	    // + " ==================");

	    for (long recordIndex = this.fromID; recordIndex <= this.toID; ++recordIndex) {

		fields = new String[6];

		fields[0] = this.userName + Integer.toString(i);
		fields[1] = this.userName + Integer.toString(i);
		fields[2] = this.userName + Integer.toString(i);
		fields[3] = "10";
		fields[4] = "$20.00";
		fields[5] = "10200300";

		contractor = new Contractor(recordIndex, fields);

		try {
		    this.remoteContractorsAccess.updateContractor(contractor);
		} catch (Exception e) {
		    // Data.log("", "????????????????????? RecordNotFoundException recordID = "
		    // + Long.toString(recordIndex));
		    e.printStackTrace();
		} finally {
		    try {
			// this.remoteContractorsAccess.unlockContractor(contractor, lockCookie);
		    } catch (Exception e) {
			e.printStackTrace();
		    }
		}
	    }
	}
    }
}

class LockUpdateUnlockUser3 extends Thread {

    private String userName;
    private String serverName;
    private int portNumber;
    private int iterations;
    private long fromID;
    private long toID;

    public LockUpdateUnlockUser3(String name, String serverName, int portNumber, long fromID, long toID, int iterations) {

	super.setName(name);
	this.serverName = serverName;
	this.portNumber = portNumber;
	this.userName = name;
	this.iterations = iterations;
	this.fromID = fromID;
	this.toID = toID;
    }

    public void run() {

	long lockCookie = -1;

	String[] fields;
	Contractor contractor;

	// RemoteContractorsAccess access;
	// try {
	// access = RmiClient.getContractorsAccess(this.serverName, this.portNumber);
	// } catch (ServerClientException e1) {
	// // Data.log("", "ServerClientException");
	// return;
	// }
	//
	// for (int i = 1; i <= this.iterations; ++i) {
	//
	// // Data.log("", "================= LockUpdateUnlockUser3 user iteration " + Integer.toString(i)
	// // + " ==================");
	//
	// for (long recordIndex = this.fromID; recordIndex <= this.toID; ++recordIndex) {
	//
	// fields = new String[6];
	//
	// fields[0] = this.userName + Long.toString(recordIndex);
	// fields[1] = this.userName + Long.toString(recordIndex);
	// fields[2] = this.userName;
	// fields[3] = "10";
	// fields[4] = "$20.00";
	// fields[5] = "10200300";
	//
	// contractor = new Contractor(recordIndex, fields);
	//
	// // try to update.
	// try {
	// access.updateContractor(contractor.copyWithEmptyOwner());
	// access.updateContractor(contractor);
	// } catch (IllegalStateException e) {
	// // Data
	// // .log("",
	// //
	// "LockUpdateUnlockUser3. updateContractor threw IllegalStateException!! will exit no unlock is necessary the client!!!");
	// return;
	// } catch (Exception e) {
	// // Data.log("", "LockUpdateUnlockUser3. updateContractor threw Exception!!");
	// }
	// }
	// }
    }
}